package com.grj.park.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.grj.park.common.R;
import com.grj.park.domain.Coupons;
import com.grj.park.domain.Promotions;
import com.grj.park.service.PromotionsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("promotions")
public class PromotionsController {

    private final PromotionsService promotionsService;

    @GetMapping
    public R<List<Promotions>> getPromotions(){
        return R.ok(promotionsService.list());
    }

    @PostMapping
    public R<?> savePromotions(@RequestBody Promotions promotions){
        LambdaQueryWrapper<Promotions> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Promotions::getPromotionName, promotions.getPromotionName());
        Promotions one = promotionsService.getOne(queryWrapper);
        if (one != null){
            return R.fail("优惠券名字已存在");
        }
        promotionsService.save(promotions);
        return R.okMsg("添加优惠券成功");
    }
}
